package galaxy.math;

import processing.core.PVector;

public class Matrix {
	PVector[] columns = new PVector[3];
	
	// Constructs a matrix given 3 arrays of 3 floats representing horizontal vectors
	public Matrix(float[] x, float[] y, float[] z) {
		// Translate horizontal vectors to vertical vectors
		this.columns[0] = new PVector(x[0], y[0], z[0]);
		this.columns[1] = new PVector(x[1], y[1], z[1]);
		this.columns[2] = new PVector(x[2], y[2], z[2]);
	}
	
	// Constructs a matrix given 3 PVectors representing vertical vectors
	public Matrix(PVector[] columns) {
		this.columns = columns;
	}
	
	// Performs matrix-matrix multiplication: this * m
	public Matrix multiply(Matrix m) {
		return new Matrix(new PVector[] {
			multiply(m.column(0)),
			multiply(m.column(1)),
			multiply(m.column(2))
		});
	}
	
	// Performs matrix-vector multiplication: this * p
	public PVector multiply(PVector p) {
		return new PVector(
			columns[0].x * p.x + columns[1].x * p.y + columns[2].x * p.z,
			columns[0].y * p.x + columns[1].y * p.y + columns[2].y * p.z,
			columns[0].z * p.x + columns[1].z * p.y + columns[2].z * p.z
		);
	}
	
	// Retrieves a vertical vector by a given index
	public PVector column(int index) {
		return this.columns[index];
	}
}
